const db = wx.cloud.database();
const articleUrl = "http://www.shengjiangwang.com/xinwen/yingshi/list_33_2.html";
const category =311;
const fs = wx.getFileSystemManager();
const reptileQuery = () => {
  //return;

  wx.request({
    url: articleUrl,
    success(res) {
      var html = res.data;
      var arr = html.match(/<div class=\"art_title\">([\s\S]*?)<\/div>/g);
      if (arr && arr.length > 0) {
        parseHtml(arr);
      }
    }
  })

}

function parseHtml(arr) {

  var url =null;
  var title = null;
  for (var i = 0; i < arr.length; i++) {
    url = arr[i].match(/http:([\s\S]*?).html/g)[0];
    title = removeHtml(arr[i]);

    addArticle(url,title);
    
  }
}

function addArticle(url, title) {
  //请求每一遍文章
  wx.request({
    url: url,
    success(res) {
      var html = res.data;
      var article = html.match(/<div class=\"articleInfo\">([\s\S]*?)<\/div>/g)[0];

      //处理一下article
      //替换strong 为span
      //<strong>Night night / Nighty night 晚安</strong>
      article = article.replace(/<strong>/g, "<font style='font-weight:bold'>");
      article = article.replace(/<\/strong>/g, "</font>");
      article = article.replace(/<img src=\"\/uploads/g, "<img src=\"http://www.shengjiangwang.com/uploads");
      article = article.replace(/&#39;/g, "'");

      //清除广告
      article = article.replace(/<p>([\s]*?)更多英语新词([\S]*?)<\/p>/g, "");
      article = article.replace(/<p>([\s]*?)更多实用英语表达([\S]*?)<\/p>/g, "");
      article = article.replace(/<p>([\s]*?)又是崭新的一周，给自己定个小目标([\S]*?)<\/p>/g, "");
      article = article.replace(/<p>([\s]*?)是不是好用又简单([\S]*?)<\/p>/g, "");

      
     
      var arr = html.match(/<p([0-9\-\?\sa-zA-Z,.'\"!:;‘’=]*?)>([0-9\-\?\sa-zA-Z,.'\"\/!:;‘’=，？！]*?)<\/p>/g);
      //var arr = html.match(/<p([0-9\-\?\sa-zA-Z,.\'\"!:;‘’=]*?)>([^\u4e00-\u9fa5]*?)<\/p>/g);
      var p = null;
      var newp = null;
      if (arr)
      {
        for (var i = 0; i < arr.length; i++) {
          p = arr[i];
          if (p.match(/<p([0-9\-\?\sa-zA-Z,.'\"!:;‘’=]*?)>([\s]*?)<\/p>/g)) {
            continue;
          }
          if (p.indexOf("<p style=") >= 0) {
            newp = p.replace("<p style=\"", "<p style=\"background-color:#333333;color:#eeeeee;padding:5px;");
          } else {
            newp = p.replace("<p", "<p style=\"background-color:#333333;color:#eeeeee;padding:5px;\"");
          }
          article = article.replace(p, newp);
        }
      }
    
     // console.info(article);
      //console.info(arr);
      db.collection('article_list').add({
        data: {
          category: category,
          title: title,
          url: "/pages/study/article/article-page",
          content: article
        }
      })
      .then(res => {
        console.log("成功插入一条。")
      })
    }
  })
}


var wordDb="";


function sleep(delay) {
  var start = (new Date()).getTime();
  while ((new Date()).getTime() - start < delay) {
    continue;
  }
}

//剔除html
function removeHtml(str) {
  str = str.replace(/<\/?.+?>/g, "");
  return str;
}
//剔除空格
function removeBlank(str) {
  str = str.replace(/\s+/g, "");
  return str;
}

module.exports = {
  reptileQuery: reptileQuery
}